﻿using System;
using System.Collections;
using System.Data;
using Unis.Framework.ORM.Mapping;
using Unis.Framework.ORM.Model;
using Unis.Framework.RuleBase.Common;
using Unis.Framework.RuleBase.Serializer;
using Unis.ORM.Mapping;
using Unis.RuleBase.Common;
using Unis.RuleBase.IFace;

namespace Unis.Framework.RuleBase
{
    public abstract class BaseQueryRule : ContextBoundObject, IBaseQueryRule
    {
        // Fields
        private Type _ObjectDataDocType;
        private QueryParameterMappings _QueryParamMapping;

        // Methods
        public BaseQueryRule(Type objectDataDocType)
        {
            this._ObjectDataDocType = objectDataDocType;
        }

        protected object ConvertDataInDocType(int value)
        {
            return Convert.ChangeType(Enum.Parse(this._ObjectDataDocType, value.ToString()), this._ObjectDataDocType);
        }

        public virtual DataSet GetObjectAsDataSet(int dataInDocType, string xmlFilterParams)
        {
            QueryParameterInfo[] parInfos = new QueryParameterXmlSerializer().DeSerializer(xmlFilterParams);
            return new ObjectEditHelper().GetObjectAsDataSet(this, this.ConvertDataInDocType(dataInDocType), parInfos, new object[0]);
        }

        public virtual IList GetObjects(int dataInDocType, string xmlFilterParams)
        {
            QueryParameterInfo[] parInfos = new QueryParameterXmlSerializer().DeSerializer(xmlFilterParams);
            return new ObjectEditHelper().GetObjects<object>(this, this.ConvertDataInDocType(dataInDocType), parInfos, new object[0]);
        }

        // Properties
        public Type ObjectDataDocType
        {
            get
            {
                return this._ObjectDataDocType;
            }
            set
            {
                this._ObjectDataDocType = value;
            }
        }

        public virtual QueryParameterMappings QueryParamMapping
        {
            get
            {
                return this._QueryParamMapping;
            }
            set
            {
                this._QueryParamMapping = value;
            }
        }
    }
}
